package com.example.utils;

import com.example.model.dos.ClubMemberDO;
import com.example.utils.DateUtil;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文件工具类
 * </p>
 * @author 34448
 */
public class FileUtils {

    /**
     * 检查文件路径是否有效
     * @param filePath 文件路径
     * @return
     */
    public static boolean isValidFilePath(String filePath) {
        File file = new File(filePath);
        return file.exists() && file.isDirectory();
    }

    /**
     * 保存文件
     * @param file 文件
     * @param directory 目录
     * @return 文件URL
     * @throws IOException
     */
    public static String saveFile(MultipartFile file, String directory) throws IOException {
        Path path = Paths.get(directory, file.getOriginalFilename());
        Files.createDirectories(path.getParent());
        Files.write(path, file.getBytes());
        return path.toString();
    }

    /**
     * 解析文件并导入社团成员信息
     * @param filePath 文件路径
     * @return 社团成员列表
     * @throws IOException
     */
    public static List<ClubMemberDO> parseClubMembersFromFile(String filePath) throws IOException {
        // 这里假设文件格式为CSV，实际可以根据具体文件格式进行解析
        List<ClubMemberDO> members = new ArrayList<>();
        List<String> lines = Files.readAllLines(Paths.get(filePath));

        for (String line : lines) {
            String[] fields = line.split(",");
            ClubMemberDO member = new ClubMemberDO();
            member.setUserId(Integer.parseInt(fields[0]));
            member.setName(fields[1]);
            member.setGender(fields[2]);
            member.setBirthDate(DateUtil.parse(fields[3]));
            member.setMajor(fields[4]);
            member.setGrade(fields[5]);
            member.setPhoneNumber(fields[6]);
            member.setAddress(fields[7]);
            member.setPhotoUrl(fields[8]);
            member.setCertificateUrl(fields[9]);
            members.add(member);
        }

        return members;
    }

    /**
     * 导出社团成员信息到指定文件路径
     * @param members 社团成员列表
     * @param filePath 文件路径
     * @throws IOException
     */
    public static void exportClubMembersToFile(List<ClubMemberDO> members, String filePath) throws IOException {
        StringBuilder sb = new StringBuilder();
        for (ClubMemberDO member : members) {
            sb.append(member.getUserId()).append(",")
                    .append(member.getName()).append(",")
                    .append(member.getGender()).append(",")
                    .append(DateUtil.format(member.getBirthDate())).append(",")
                    .append(member.getMajor()).append(",")
                    .append(member.getGrade()).append(",")
                    .append(member.getPhoneNumber()).append(",")
                    .append(member.getAddress()).append(",")
                    .append(member.getPhotoUrl()).append(",")
                    .append(member.getCertificateUrl()).append("\n");
        }

        Files.write(Paths.get(filePath), sb.toString().getBytes());
    }
}